<template>
    <div>
        <a-form :form="form" layout="inline" @submit.prevent="onSubmit" id="goodsCateSearch"
                :style="{marginBottom: '20px'}">
            <div class="goods-cate-query">
                <a-form-item>
                    <a-input placeholder="分类名称" v-decorator="['name']"/>
                </a-form-item>
                <a-form-item :style="{flexGrow: 1}">
                    <a-button type="primary" html-type="submit">
                        筛选
                    </a-button>
                </a-form-item>
                <a-form-item>
                    <a-button type="primary" v-if="menu_role.includes('/goodscate/create')" icon="plus"
                              @click="showModal">添加
                    </a-button>
                </a-form-item>
            </div>
        </a-form>
        <a-table :columns="columns" :dataSource="tableData" rowKey="id" :pagination="pagination" bordered>
            <span slot="image" slot-scope="text, record">
                <img :src="record.img" style="width:64px;height:64px" alt="图片">
            </span>
            <span slot="action" slot-scope="text, record">
                <div class="delete-image">
                    <router-link :to="'/goodscate/update/'+record.id"
                                 v-if="menu_role.includes('/goodscate/update')">
                        <img src="./../../assets/images/view.svg" alt="">
                    </router-link>
                    <a href="#" v-if="menu_role.includes('/goodscate/delete')"
                       @click.stop.prevent="handleDelete(record)">
                        <img src="./../../assets/images/delete.svg" alt="">
                    </a>
                </div>
            </span>
        </a-table>
        <a-modal
                title="添加产品分类"
                :visible="visible"
                @ok="onAddSubmit"
                @cancel="handleCancel"
                centered
                :cancelButtonProps="{ props: {type: 'primary'}}"
                :keyboard='false'
                okText='确认'
                :maskClosable='false'
        >
            <create ref="create" @getList="getList"  @hideCreate="hideCreate"></create>
        </a-modal>
    </div>
</template>

<script>
    import Create from "./create";

    export default {
        components: {Create},
        data() {
            return {
                pagination: {
                    current: 1,
                    total: 0,
                    pageSize: 10,
                    showQuickJumper: true,
                    onChange: (page, size) => {
                        this.handleChange(page, size)
                    },
                },
                tableData: [],
                columns: [
                    {title: 'ID', dataIndex: 'id', align: 'center'},
                    {title: '分类名称', dataIndex: 'name', align: 'center'},
                    {title: '图片', scopedSlots: {customRender: 'image'}, align: 'center'},
                    {title: '计算参数', dataIndex: 'param', align: 'center'},
                    {title: '状态', dataIndex: 'status', align: 'center'},
                    {title: '操作', scopedSlots: {customRender: 'action'}, align: 'center'},
                ],
                form: this.$form.createForm(this),
                search: {},
                visible: false,
            }
        },
        mounted() {
            this.getList();
            
        },
        methods: {
            async getList(param = {}) {
                let req = {page_size: this.pagination.pageSize, page: this.pagination.current};

                let data = await this.http(
                    "get",
                    "goodscate/index",
                    Object.assign(req, param)
                );
                this.tableData = data.data;
                this.pagination.total = data.total_count;
            },
            onSubmit() {//搜索提交
                this.form.validateFields((err, values) => {
                    if (!err) {
                        this.pagination.current = 1;
                        this.search = values;
                        this.getList(values)
                    }
                })
            },
            handleChange(page, size) {
                //列表展示条数改变
                this.pagination.current = page;
                this.pagination.pageSize = size;
                this.getList(this.search);
            },
            handleDelete(row) {
                var that = this;
                this.$confirm({
                    title: '删除产品分类',
                    content: '你确定要删除该产品分类吗？',
                    onOk() {
                        that.deleteData(row.id).then(res => {
                            if (res.status == "success") {
                                that.$message.success("删除成功!", 1);
                                that.getList();
                            } else {
                                that.$message.error("删除失败!", 1);
                            }
                        });
                    },
                    onCancel() {
                    },
                });
            },
            async deleteData(id) {
                return await this.http("delete", "goodscate/delete", {id: id});
            },
            showModal() {
                this.visible = true
            },
            hideCreate() {
                this.visible = false
            },
            handleCancel() {
                this.visible = false;
                this.$refs.create.form.resetFields();
                this.$refs.create.image_url = ''
            },
            
            onAddSubmit(form) {
                this.$refs.create.onSubmit()
            },
        }
    };
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    @import "./../../assets/scss/mixin";

    #goodsCateSearch {
        .ant-input {
            width: 250px;
        }
    }

    .goods-cate-query {
        @include fj(space-between);

        .ant-form-item + .ant-form-item {
            margin-left: 10px;
        }
    }
</style>
